import React from 'react'
import { Routes, Route, Navigate } from 'react-router-dom'
import { Layout } from 'antd'
import AppLayout from './components/Layout/AppLayout'
import ProtectedRoute from './components/ProtectedRoute'
import Login from './pages/Login'
import Register from './pages/Register'
import UserProfile from './pages/UserProfile'
import UserManagement from './pages/UserManagement'
import Dashboard from './pages/Dashboard'
import TaskManagement from './pages/TaskManagement'
import JobManagement from './pages/JobManagement'
import SystemStatus from './pages/SystemStatus'
import { authUtils } from './services/auth'

const { Content } = Layout

function App() {
  return (
    <div className="App">
      <Routes>
        {/* 公开路由 */}
        <Route 
          path="/login" 
          element={
            authUtils.isAuthenticated() ? 
              <Navigate to="/" replace /> : 
              <Login />
          } 
        />
        <Route 
          path="/register" 
          element={
            authUtils.isAuthenticated() ? 
              <Navigate to="/" replace /> : 
              <Register />
          } 
        />
        
        {/* 受保护的路由 */}
        <Route 
          path="/*" 
          element={
            <ProtectedRoute>
              <AppLayout>
                <Layout.Content style={{ padding: '24px', minHeight: 'calc(100vh - 64px)' }}>
                  <Routes>
                    <Route path="/" element={<Dashboard />} />
                    <Route path="/tasks" element={<TaskManagement />} />
                    <Route path="/jobs" element={<JobManagement />} />
                    <Route 
                      path="/system" 
                      element={
                        <ProtectedRoute requiredRole="admin">
                          <SystemStatus />
                        </ProtectedRoute>
                      } 
                    />
                    <Route 
                      path="/users" 
                      element={
                        <ProtectedRoute requiredRole="admin">
                          <UserManagement />
                        </ProtectedRoute>
                      } 
                    />
                    <Route path="/profile" element={<UserProfile />} />
                    {/* 默认重定向到仪表盘 */}
                    <Route path="*" element={<Navigate to="/" replace />} />
                  </Routes>
                </Layout.Content>
              </AppLayout>
            </ProtectedRoute>
          } 
        />
      </Routes>
    </div>
  )
}

export default App